﻿#include "NPPPySideMgrUI.h"
#include "ui_NPPPySideMgrUI.h"
#include "NPPPySide.h"

NPPPySideMgrUI::NPPPySideMgrUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NPPPySideMgrUI)
{
    ui->setupUi(this);
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Xairy.Co.Ltd", "NPPPSide");
    settings.setValue("last", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
    settings.sync();

    QString filePath = QFileInfo(settings.fileName()).absolutePath() + "/master.py";
    if (!QFile::exists(filePath)) {
        QFile fileMaster(filePath);
        fileMaster.open(QIODevice::WriteOnly);
        fileMaster.write("from PySide2 import QtCore\r\n\r\nif __name__ == '__main__':\r\n    import sys\r\n    #code here");
        fileMaster.close();
    }

    loadAllItems();
}

NPPPySideMgrUI::~NPPPySideMgrUI()
{
    delete ui;
}


void NPPPySideMgrUI::on_btnAddScript_clicked()
{
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Xairy.Co.Ltd", "NPPPSide");
    QString lastPath = settings.value("last_select_path").toString();
    if (lastPath.isEmpty()) {
        QString desktopPath = QFileInfo(qgetenv("USERPROFILE") + "/Desktop").absoluteFilePath();
        lastPath = desktopPath;
    }

    QString fileScript = QFileDialog::getOpenFileName(
                this, QString::fromWCharArray(L"选择一个脚本添加"),
                lastPath, "Python Files (*.py)");

    if (fileScript.isEmpty())
        return;

    QString filePath = QFileInfo(settings.fileName()).absolutePath();
    QFileInfo fileInfoScript(fileScript);
    QString uniqueId = QUuid::createUuid().toRfc4122().toHex().mid(0, 16);
    QString uniqueName = fileInfoScript.baseName() + uniqueId + ".py";

    QString fileDest = filePath + "/" + uniqueName;
    if (!QFile::copy(fileScript, fileDest))
        return;

    settings.setValue("last_select_path", fileInfoScript.absolutePath());
    settings.sync();

    QString dtStr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    settings.beginGroup(uniqueId);
    settings.setValue("name", fileInfoScript.fileName());
    settings.setValue("file", fileDest);
    settings.setValue("index", ui->tableWidgetScripts->rowCount());
    settings.setValue("create", dtStr);
    settings.setValue("last", dtStr);
    settings.setValue("enabled", true);
    settings.endGroup();
    settings.sync();


    int row = 0;
    ui->tableWidgetScripts->insertRow(0);
    QTableWidgetItem *item = nullptr;
    ui->tableWidgetScripts->setItem(row, 0, new QTableWidgetItem(fileInfoScript.fileName()));
    item = new QTableWidgetItem(true ? QString::fromWCharArray(L"启动") : QString::fromWCharArray(L"禁用"));
    item->setTextAlignment(Qt::AlignCenter);
    ui->tableWidgetScripts->setItem(row, 1, item);
    item = new QTableWidgetItem(dtStr);
    item->setTextAlignment(Qt::AlignCenter);
    ui->tableWidgetScripts->setItem(row, 2, item);
    item = new QTableWidgetItem(dtStr);
    item->setTextAlignment(Qt::AlignCenter);
    ui->tableWidgetScripts->setItem(row, 3, item);
    ui->tableWidgetScripts->setItem(row, 4, new QTableWidgetItem(fileDest));

    NPPPySide::instance()->UpdateMenu();
}

void NPPPySideMgrUI::loadAllItems()
{
    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Xairy.Co.Ltd", "NPPPSide");
    QMap<int, QString> groupsSort;
    QStringList groups = settings.childGroups();
    for (int i = 0; i < groups.count(); ++i) {
        const QString &group = groups.at(i);
        if (group == "[General]")
            continue;
        settings.beginGroup(group);
        int index = settings.value("index").toInt();
        groupsSort.insert(index, group);
        settings.endGroup();
    }

    int index = 0;
    for (auto itr = groupsSort.begin(); itr != groupsSort.end(); ++itr, index++) {
        settings.beginGroup(itr.value());
        QString scriptName = settings.value("name").toString();
        QString scpritFile = settings.value("file").toString();
        bool enabled = settings.value("enabled").toBool();
        QString dtCreateStr = settings.value("create").toString();
        QString dtLastStr = settings.value("last").toString();

        int row = ui->tableWidgetScripts->rowCount();
        ui->tableWidgetScripts->insertRow(row);
        QTableWidgetItem *item = nullptr;
        ui->tableWidgetScripts->setItem(row, 0, new QTableWidgetItem(scriptName));
        item = new QTableWidgetItem(enabled ? QString::fromWCharArray(L"启动") : QString::fromWCharArray(L"禁用"));
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidgetScripts->setItem(row, 1, item);
        item = new QTableWidgetItem(dtCreateStr);
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidgetScripts->setItem(row, 2, item);
        item = new QTableWidgetItem(dtLastStr);
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidgetScripts->setItem(row, 3, item);
        ui->tableWidgetScripts->setItem(row, 4, new QTableWidgetItem(scpritFile));
        settings.endGroup();
    }

}

void NPPPySideMgrUI::on_btnMasterFile_clicked()
{
    NPPPySide::instance()->OpenMasterFile();
}
